<template>
	<view class="address">
		<!-- #ifdef APP-PLUS -->
		<Hearder :name="'收货地址'"></Hearder>
		<!-- #endif -->
		<u-transition :show="show">
			<view class="address_m">
				<view class="address_list bgbottom" v-for="item in list" :key="item.address_id">
					<view class="box_top" @click="backhistory(item.address_id)">
						<view>
							<text>收货人：{{item.name}}</text>
							<text>{{item.phone}}</text>
						</view>
						<view>{{item.detail}}</view>
					</view>
					<view class="box_bot">
						<view @click="moren(item.address_id,item.is_default)">
							<view class="create" :class="item.is_default==1?'active':''">
								<text v-if="item.is_default!=1" class="duihao"></text>
								<text v-else>✓</text>
								默认地址
							</view>
						</view>
						<view class="add_r">
							<view @click="toPage('addaddress?id='+item.address_id+'&select_status='+item.select_status)">
								<image src="../../static/images/edit.png"></image>
								<text>编辑</text>
							</view>
							<view @click="del(item.address_id)">
								<u-icon name="trash" color="#AAAAAA" size="18"></u-icon>
								<text>删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-skeleton
				rows="3"
				:title="false"
				:loading="loading"
				:rowsHeight="['50rpx', '50rpx', '50rpx']"
				:rowsWidth="['100%', '100%', '100%']"
				animate
			></u-skeleton>
			<view class="notaddress" v-if="list.length==0&&!loading">
				<image src="/static/images/notaddress.png" mode="widthFix"></image>
			</view>
			<view class="address_b" @click="toPage('addaddress')">添加新地址</view>
		</u-transition>
	</view>
</template>

<script>
	export default {
		data(){
			return{
				list:[],
				type:0,
				show:true,
				loading:true
			}
		},
		onShow() {
			this.getinfo()
		},
		onLoad(options){
			this.type=options.type
		},
		methods:{
			getinfo(){
				this.request('/address/getList').then(res => {
					if(res.data.code==1){
						this.list=res.data.data
					}
					this.loading = false;
				})
			},
			toPage(route){
				uni.navigateTo({
					url: route
				});
			},
			moren(id,select_status){
				if(!select_status){
					this.request('/address/setDefault',{address_id:id,is_default:1}).then(res => {
						if(res.data.code == 1){
							 this.$tip(res.data.msg)
							 this.getinfo()
						}else{
							this.$tip(res.data.msg)
						}
					})
				}else{
					this.$tip("已经是默认地址了~")
				}
			},
			del(id){
				this.request('/address/del',{address_id:id}).then(res => {
					if(res.data.code == 1){
						 this.$tip(res.data.msg)
						 this.getinfo()
					}else{
						this.$tip(res.data.msg)
					}
				})
			},
			backhistory(id){
				if(this.type==1){
					this.request('/address/setDefault',{address_id:id,is_default:1}).then(res => {
						uni.navigateBack()
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.address{
		.address_m{
			padding-bottom: 100rpx;
			.address_list{
				padding: 0 3%;
				.box_top{
					view{
						&:nth-of-type(1){
							display: flex;
							justify-content: space-between;
							height: 90rpx;
							line-height: 90rpx;
						}
						&:nth-of-type(2){
							font-size: 28rpx;
							color: #AAA;
							padding-bottom:20rpx;
							border-bottom: 2rpx solid #F5F5F5;
						}
					}
				}
				.box_bot{
					line-height: 80rpx;
					height: 80rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
					.create{
						font-size: 26rpx;
						text{
							position: relative;
							display: inline-block;
							width: 26rpx;
							height: 26rpx;
							border: 2rpx solid #323232;
							border-radius: 4rpx;
							margin-right: 20rpx;
							text-align: center;
							line-height: 26rpx;
							font-size: 24rpx;
						}
						.duihao{
							top: 4rpx;
						}
					}
					.active {
						text{
							color: $uni-text-color;
							font-weight: 800;
							border: 2rpx solid $uni-text-color;
						}
					}
					.add_r{
						display: flex;
						justify-content: space-between;
						view{
							margin-left: 40rpx;
							text{
								color: #3C3C3C;
								font-size: 26rpx;
							}
							image{
								width: 30rpx;
								height: 30rpx;
								vertical-align: middle;
								margin-right: 20rpx;
								margin-top: -4rpx;
							}
							.u-icon{
								margin-right: 10rpx;
								top: 4rpx;
							}
						}
					}
				}
			}
		}
		.address_b{
			position: fixed;
			width: 100%;
			bottom: 0;
			background: $uni-item-color;
			text-align: center;
			line-height: 97rpx;
			height: 97rpx;
			color: #fff;
		}
		.notaddress{
			image{
				width: 40%;
				margin: 300rpx 30% 0;
			}
		}
	}
</style>
